package com.jzh.selfwallpaper.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.jzh.selfwallpaper.R;
import com.jzh.selfwallpaper.constant.Constant;
import com.jzh.selfwallpaper.entity.WallpaperListItem;
import com.jzh.selfwallpaper.util.ScreenShootUtil;
import com.jzh.selfwallpaper.view.activity.ScreenShootActivity;
import com.jzh.selfwallpaper.view.adapter.commonlistview.CommonListViewAdapter;
import com.jzh.selfwallpaper.view.adapter.commonlistview.ListViewHolder;
import com.jzh.selfwallpaper.view.adapter.commonrecyclerview.BaseRecyclerHolder;
import com.jzh.selfwallpaper.view.adapter.commonrecyclerview.CommonRecyclerViewAdapter;
import com.jzh.selfwallpaper.view.widget.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class Fragment_home extends BaseFragment {

    private CommonRecyclerViewAdapter<WallpaperListItem> commonRecyclerViewAdapter;
    private RecyclerView rv_home_list;
    private List<WallpaperListItem> list = new ArrayList<>();

    @Override
    protected int setView() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init(View view) {
        rv_home_list = view.findViewById(R.id.rv_home_list);
        commonRecyclerViewAdapter = new CommonRecyclerViewAdapter<WallpaperListItem>(getActivity(), list, R.layout.item_home_rv) {
            @Override
            public void convert(BaseRecyclerHolder holder, WallpaperListItem item, int position, boolean isScrolling) {
                Log.i("iiiooo","item:"+item.toString());
                holder.setImageByUrl(R.id.iv_home_item_image, item.url);
                holder.setText(R.id.tv_home_item_title, item.name);
            }
        };
        rv_home_list = getActivity().findViewById(R.id.rv_home_list);
        //纵向线性布局
        //LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        SpaceItemDecoration spaceItemDecoration = new SpaceItemDecoration(20);
        //纵向线性布局
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        rv_home_list.setLayoutManager(layoutManager);
        rv_home_list.addItemDecoration(spaceItemDecoration);
        rv_home_list.setAdapter(commonRecyclerViewAdapter);

        commonRecyclerViewAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Intent intent = new Intent(getActivity(), ScreenShootActivity.class);
                intent.putExtra(Constant.MODEL_NUMBER, list.get(position).number);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        WallpaperListItem wallpaperListItem0 = new WallpaperListItem();
        wallpaperListItem0.number = 0x0001;
        wallpaperListItem0.url = "https://thumbnail0.baidupcs.com/thumbnail/6edd4ea31823d1e99f58d2ba5c554b66?fid=1132930-250528-563240388687031&time=1540886400&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-pelIlCVL2W1ZOW9aPy7ydQfCWek%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=102492960738359124&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video";
        wallpaperListItem0.name = "姓名壁纸";
        list.add(wallpaperListItem0);
        WallpaperListItem wallpaperListItem1 = new WallpaperListItem();
        wallpaperListItem1.number = 0x0002;
        wallpaperListItem1.url = "https://thumbnail0.baidupcs.com/thumbnail/8e7ac7debd3f4bec47d4e637090122e4?fid=1132930-250528-445405174425864&time=1540886400&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-byiENwQIPaRZwMD79BLUXyse0to%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=102422248159610572&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video";
        wallpaperListItem1.name = "文艺壁纸";
        list.add(wallpaperListItem1);
        WallpaperListItem wallpaperListItem2 = new WallpaperListItem();
        wallpaperListItem2.number = 0x0003;
        wallpaperListItem2.url = "https://thumbnail0.baidupcs.com/thumbnail/8e7ac7debd3f4bec47d4e637090122e4?fid=1132930-250528-445405174425864&time=1540886400&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-byiENwQIPaRZwMD79BLUXyse0to%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=102422248159610572&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video";
        wallpaperListItem2.name = "胶片壁纸";
        list.add(wallpaperListItem2);
        WallpaperListItem wallpaperListItem = new WallpaperListItem();
        wallpaperListItem.number = 0x0000;
        wallpaperListItem.url = null;
        wallpaperListItem.resId = R.drawable.add;
        wallpaperListItem.name = "自定义壁纸";
        list.add(wallpaperListItem);
    }
}
